//
//  DropdownTipButton.m
//  Etion
//
//  Created by wu jingxing on 12-2-12.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "DropdownTipButton.h"

#import "UIImageExtend.h"

@implementation CDropdownTipButton

@synthesize delegate = _delegate;
@synthesize textLabel = _textLabel;
@synthesize alignment = _alignment;

- (id)initWithFrame:(CGRect)frame type:(EDropdownTipButtonType)type
{
    self = [super initWithFrame:frame];

    self.backgroundColor = [UIColor clearColor];

    switch ((NSInteger) type)
    {
        case EDropdownTipButtonTypeTitle:
        {
            _arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_dropdowninstruct.png"]];
            _arrow.center = CGPointMake(_arrow.center.x, CGRectGetHeight(self.frame) / 2);
            CGRect frame = _arrow.frame;
            frame.origin.x = CGRectGetWidth(self.frame) - CGRectGetWidth(frame);
            _arrow.frame = frame;

            _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame) - 5 - CGRectGetWidth(_arrow.frame), CGRectGetHeight(self.frame))];
            _textLabel.textColor = [UIColor whiteColor];
            _textLabel.font = [UIFont systemFontOfSize:17];
            _textLabel.backgroundColor = [UIColor clearColor];
            _textLabel.textAlignment = NSTextAlignmentCenter;

            [self addSubview:_arrow];
            [self addSubview:_textLabel];

            break;
        }
        default:
            break;
    }

    return self;
}

- (void)setAlignment:(EDropdownTipButtonContentAlignment)alignment
{
    _alignment = alignment;

    CGSize size = [_textLabel.text stringSizeWithFont:_textLabel.font];
    CGFloat nWidth = self.width - 5 - _arrow.width - (_headImage != nil? 5 + _headImage.width : 0);
    CGFloat min = MIN(nWidth, size.width);
    nWidth = (self.width - (min + 5 + _arrow.width + (_headImage != nil? 5 + _headImage.width : 0))) / 2;

    switch ((NSInteger) alignment)
    {
        case EDropdownTipButtonContentAlignmentCenter:
        {
            CGRect frame = _textLabel.frame;
            frame.size.width = min;
            frame.origin.x = nWidth + (_headImage != nil? 5 + _headImage.width : 0);
            _textLabel.frame = frame;

            frame = _arrow.frame;
            frame.origin.x = _textLabel.right + 5;
            _arrow.frame = frame;

            frame = _headImage.frame;
            frame.origin.x = nWidth;
            _headImage.frame = frame;
            break;
        }
        case EDropdownTipButtonContentAlignmentLeft:
        {
            if (_headImage != nil)
                _headImage.left = 0;

            CGRect frame = _textLabel.frame;
            frame.size.width = min;
            frame.origin.x = _headImage != nil? _headImage.right + 5 : 0;
            _textLabel.frame = frame;

            _arrow.left = _textLabel.right + 5;

            break;
        }
        case EDropdownTipButtonContentAlignmentRight:
        {
            _arrow.right=self.width;
            
            _textLabel.right=_arrow.left-5;
            
            break;
        }
    }
}

- (void)setText:(NSString *)szText animate:(BOOL)bAnimate
{
    void(^fun)() = ^
    {
        _textLabel.text = szText;

        self.alignment = _alignment;
    };
    if (YES == bAnimate)
    {
        [UIView animateWithDuration:0.3 animations:^
        {
            fun();
        }];
    }
    else
        fun();
}

- (void)setImage:(UIImage *)image animate:(BOOL)bAnimate
{
    if (image != nil)
    {
        if (_headImage == nil)
        {
            _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
            _headImage.alpha = 0;
            [self addSubview:_headImage];
        }
        _headImage.image = image;

        CGFloat nWidth = 0;
        CGFloat nHeight = 0;
        [UIImage caleFitImageSize:image.size targetSize:CGSizeMake(self.height, self.height) width:&nWidth height:&nHeight];
        _headImage.frame = CGRectMake(_headImage.left, _headImage.top, nWidth, nHeight);
        _headImage.center = CGPointMake(_headImage.center.x, self.height / 2);
    }
    else
    {
        [_headImage removeFromSuperview];
        [_headImage release_mm];
        _headImage = nil;
    }

    CGSize size = [_textLabel.text stringSizeWithFont:_textLabel.font];
    CGFloat nWidth = self.width - 5 - _arrow.width - (_headImage != nil? 5 - _headImage.width : 0);
    CGFloat min = MIN(nWidth, size.width);
    nWidth = (self.width - (min + 5 + _arrow.width + (_headImage != nil? 5 + _headImage.width : 0))) / 2;

    void(^fun)() = ^
    {
        CGRect frame = _textLabel.frame;
        frame.size.width = min;
        frame.origin.x = nWidth + (_headImage != nil? 5 + _headImage.width : 0);
        _textLabel.frame = frame;

        frame = _arrow.frame;
        frame.origin.x = _textLabel.right + 5;
        _arrow.frame = frame;

        frame = _headImage.frame;
        frame.origin.x = nWidth;
        _headImage.frame = frame;

        _headImage.alpha = 1;
    };

    if (YES == bAnimate)
    {
        [UIView animateWithDuration:0.3 animations:^
        {
            fun();
        }];
    }
    else
        fun();
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (YES == CGRectContainsPoint(self.bounds, [touch locationInView:self]))
    {
        [_delegate dropdownTipButtonClick:self];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];

    if (_textLabel != nil&& _arrow != nil)
    {
        self.alignment = _alignment;
    }
}

- (void)setArrowImage:(UIImage *)image animate:(BOOL)bAnimate
{
    if(_arrow.image==image)
        return;
    
    UIImageView *arrow = [[[UIImageView alloc] initWithImage:image] autorelease_mm];
    if(_arrow.image!=nil)
        arrow.center = _arrow.center;
    else
    {
        arrow.center = CGPointMake(arrow.center.x, self.height / 2);
        CGRect frame = arrow.frame;
        frame.origin.x = self.width - CGRectGetWidth(frame);
        arrow.frame = frame;
    }
    [self insertSubview:arrow belowSubview:_arrow];

    if (bAnimate == YES)
    {
        self.userInteractionEnabled = NO;
        UIImageView *tmp=_arrow;
        _arrow=[arrow retain_mm];
        arrow=tmp;
        _arrow.alpha = 0;
        [UIView animateWithDuration:0.3 animations:^
        {
            self.alignment = _alignment;
            _arrow.alpha = 1;
            arrow.alpha = 0;
        }                completion:^(BOOL finished)
        {
            self.userInteractionEnabled = YES;
            [arrow removeFromSuperview];
            [arrow release_mm];
        }];
    }
    else
    {
        [_arrow removeFromSuperview];
        [_arrow release_mm];
        _arrow = [arrow retain_mm];
        self.alignment = _alignment;
    }
}

- (void)rotationArrowImage
{
    [UIView animateWithDuration:0.3 animations:^
    {
         _arrow.transform = CGAffineTransformRotate(_arrow.transform, M_PI);
    } completion:^(BOOL finished)
    {
    }];
}

- (void)dealloc
{
    [_headImage release_mm];
    [_arrow release_mm];
    [_textLabel release_mm];
    superdealloc_mm;
}


@end
